namespace net.podhola.gtd.finances

import System
import System.Collections
import net.podhola
import net.podhola.gtd

[Castle.ActiveRecord.ActiveRecord]
class Account(Castle.ActiveRecord.ActiveRecordBase of Account):
	_id as int	
	[Castle.ActiveRecord.PrimaryKey(Castle.ActiveRecord.PrimaryKeyType.Increment)]
	Id as int:
		get: return _id
		set: _id = value

	_name as string	
	[Castle.ActiveRecord.Property] 
	Name as string:
		get: return _name
		set: _name = value
						
	_expenses as IList = ArrayList()
	[Castle.ActiveRecord.HasMany(Expense, Table:"Expense", ColumnKey:"accountid", Inverse:true) ] 
	Expenses as IList:
		get: return _expenses
		set: _expenses = value

	_enteredBills as IList = ArrayList()
	[Castle.ActiveRecord.HasMany(EnteredBill, Table:"EnteredBill", ColumnKey:"accountid", Inverse:true) ] 
	EnteredBills as IList:
		get: return _enteredBills
		set: _enteredBills = value

	_balances as IList = ArrayList()
	[Castle.ActiveRecord.HasMany(Balance, Table:"Balance", ColumnKey:"accountid", Inverse:true) ] 
	Balances as IList:
		get: return _balances
		set: _balances = value

	_receipts as IList = ArrayList()
	[Castle.ActiveRecord.HasMany(Receipt, Table:"Receipt", ColumnKey:"accountid", Inverse:true) ] 
	Receipts as IList:
		get: return _receipts
		set: _receipts = value

	_plannedIncomes as IList = ArrayList()
	[Castle.ActiveRecord.HasMany(Receipt, Table:"PlannedIncome", ColumnKey:"accountid", Inverse:true) ] 
	PlannedIncomes as IList:
		get: return _plannedIncomes
		set: _plannedIncomes = value

	def SortByDate():
		dc = DateComparer()
		(_expenses as ArrayList).Sort( dc )
		(_enteredBills as ArrayList).Sort( dc )

	def CashFlowInterval( StartDateTime as DateTime, EndDateTime as DateTime):
		SortByDate()
		days = ( EndDateTime - StartDateTime ).Days
		data as (ArrayList) = array(ArrayList,days)
		for i in range( data.Length ):
			data[ i ] = ArrayList()
		for expense as Expense in Expenses:			 
			if expense.SpentOn > StartDateTime:
				day = ( expense.SpentOn - StartDateTime ).Days
				if day >= 0 and day <= data.Length:
					data[ day ].Add( expense )
		for receipt as Receipt in Receipts:			 
			if receipt.ReceivedOn > StartDateTime:
				day = ( receipt.ReceivedOn - StartDateTime ).Days
				if day >= 0 and day <= data.Length:
					data[ day ].Add( receipt )
					
		return data

	def PrepareInitialBalance( AsOf as DateTime, amount as double ) as Balance:
		b = Balance()
		b.OnAccount = self
		b.BalancedOn = AsOf
		b.Amount = amount
		
		return b

	def PrepareActualBalance( amount as double ) as Balance:
		b = Balance()
		b.OnAccount = self
		b.BalancedOn = DateTime.Now
		b.Amount = amount
		
		return b
		
	def GetLastBalance() as Balance:
		lastD = DateTime.MinValue
		for b as Balance in Balances:
			if b.BalancedOn > lastD:
				lastD = b.BalancedOn
				lastB = b
		
		return lastB		
	
	def GetActualValue() as double:
		lastB = GetLastBalance()
		lastValue = lastB.Amount
		StartDateTime = lastB.BalancedOn
		for expense as Expense in Expenses:			 
			if expense.SpentOn > StartDateTime:
				lastValue -= expense.Amount
		for receipt as Receipt in Receipts:			 
			if receipt.ReceivedOn > StartDateTime:
				lastValue += receipt.Amount
		
		return lastValue
